package com.kenliang.javase.serialize;

import com.kenliang.javase.client.ZkClient;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

import java.io.Serializable;

/**
 * @author KenLiang
 * @description:
 * @date Created in 9:53 AM 2019/3/14.
 */
public class User implements Serializable {
    private static final long serialVersionUID = 8294180014912103005L;

    private String username;
    private transient String passwd;
    private static String  staticField = "serialize";

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPasswd() {
        return passwd;
    }

    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }

}

class Test{
    public static void main(String[] args) throws Exception {
        User user =  new User();
        user.setUsername("user_name");
        user.setPasswd("pass_wd");

        byte[] userBytes = ZkClient.serialize(user);
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,3);

        CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString("localhost:2181")
                .sessionTimeoutMs(5000)
                .connectionTimeoutMs(5000)
                .retryPolicy(retryPolicy)
                .namespace("serialize")
                .build();
        client.start();
        if(client.checkExists().forPath("/user")==null){
            client.create()
                    .withMode(CreateMode.PERSISTENT)
                    .forPath("/user",userBytes);
        }else{
            client.setData().forPath("/user",userBytes);
        }

    }
}

